//
//  LGSendEvaluationController.m
//  HeartDisease_YHJK
//
//  Created by Bovin on 2025/11/4.
//  聊天咨询 -- 发送评估

#import "LGSendEvaluationController.h"

@interface LGSendEvaluationController ()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIButton *sendButton;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , strong)NSMutableArray *selectArray;

@end

@implementation LGSendEvaluationController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"问卷评估";
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.sendButton];
    [self emptyViewWithSuperView:self.view frame:self.tableView.frame];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/im/scaleList" parameters:@{@"patient_id":self.patientid,@"level_id":self.levelid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if (code == 200) {
            self.dataArray = returnData;
        }
        [self.tableView reloadData];
        if (self.dataArray.count == 0) {
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无数据" offset:0];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if (self.dataArray.count == 0) {
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无数据" offset:0];
        }
    }];
}

-(void)sendButtonAction{
    if(self.selectArray.count>0){
        [RequestUtil withPOST:@"/doctor/im/sendScaleRecommendMessage" parameters:@{@"appointment_id":self.appointment_id,@"timID":self.timID,@"scale_ids":self.selectArray} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
            if(code == 200){
                [self.navigationController popViewControllerAnimated:YES];
            }
        } error:^(NSError *error) {
            
        }];
    }else{
        [TooltipView showMessage:@"请选择量表" offset:0];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGSendEvaluationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = self.dataArray[indexPath.row];
    NSString *evaid = LGNSString(self.dataArray[indexPath.row][@"id"]);
    cell.isSelected = [self.selectArray containsObject:evaid]?YES:NO;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dataDic = self.dataArray[indexPath.row];
    NSString *evaid = LGNSString(dataDic[@"id"]);
    if ([self.selectArray containsObject:evaid]) {
        [self.selectArray removeObject:evaid];
    }else{
        [self.selectArray addObject:evaid];
    }
    [self.tableView reloadData];
}

-(NSMutableArray *)selectArray{
    if(!_selectArray){
        _selectArray = [NSMutableArray array];
    }
    return _selectArray;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight-viewPix(86)) style:UITableViewStylePlain];
        [_tableView registerClass:[LGSendEvaluationCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = viewPix(153);
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

-(UIButton *)sendButton{
    if(!_sendButton){
        _sendButton = [UIButton buttonWithTitle:@"确认发送" topMargin:kMaxY(self.tableView.frame)+viewPix(5)];
        [_sendButton addTarget:self action:@selector(sendButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sendButton;
}



@end


#pragma mark
#pragma mark ====> LGSendEvaluationCell

@interface LGSendEvaluationCell()
/**   */
@property (nonatomic , strong)UIImageView *iconView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;

@end

@implementation LGSendEvaluationCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.type = LGPositionTypeSingle;
        [self createSubView];
    }
    return  self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    [super setDataDic:dataDic];
    self.titleLabel.text = dataDic[@"title"];
}

-(void)setIsSelected:(BOOL)isSelected{
    _isSelected = isSelected;
    NSString *iconName = (isSelected==NO)?@"unselect24":@"selected24";
    self.iconView.image = [UIImage imageNamed:iconName];
}

-(void)createSubView{
    [self.baseView addSubview:self.iconView];
    [self.baseView addSubview:self.titleLabel];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.baseView).offset(viewPix(16));
        make.left.equalTo(self.baseView).offset(viewPix(47));
        make.right.equalTo(self.baseView).offset(-viewPix(12));
        make.bottom.equalTo(self.baseView).offset(-viewPix(16));
    }];
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.baseView).offset(viewPix(15));
        make.centerY.equalTo(self.titleLabel);
        make.width.height.equalTo(@(20));
    }];
}



-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _titleLabel;
}


-(UIImageView *)iconView{
    if(!_iconView){
        _iconView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"unselect24"]];
    }
    return _iconView;
}



@end
